package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author panweiwei
 */
@RestController
public class FileUploadController {
    @PostMapping("/upload")
    private Result<String> upload(MultipartFile file) throws IOException {
        //把文件内容存储到本地磁盘
        String filename = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() + filename.substring(filename.lastIndexOf("."));
//        file.transferTo(new File("/Users/panweiwei/Desktop/work/Code" + fileName));
        String url = AliOssUtil.uploadFile(fileName, file.getInputStream());
        return Result.success(url);
    }
}
